﻿using Graphviz4Net.WPF.Example;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace LogAnalyzer
{
    public class Node : GraphComponent
    {
        private List<List<Node>> _table; 

        public string nodeId { get; set; }
        public string address { get; set; }
        public Leaf leaf { get; set; }
        public List<List<Node>> table 
        {
            get { return _table; }
            set 
            {
                _table = value;
                tableData = new Table(_table);
            } 
        }

        public bool MissingFromTable { get; set; }
        public bool MissingFromLeaf { get; set; }

        public string missingNode { get; set; }

        public string Color { get; set; }

        public Table tableData { get; set; }

        public Node()
        {
            this.Color = "Black";
            this.MissingFromTable = false;
            this.MissingFromLeaf = false;
        }

        public override bool Equals(object obj)
        {
            var node = obj as Node;
            return node != null && this.address == node.address;
        }

        public string Name
        {
            get { return this.address.Substring(this.address.LastIndexOf("/") + 1); }
        }

        private ICommand _leafCommand;
        public ICommand LeafCommand
        {
            get
            {
                return _leafCommand ?? (_leafCommand = new CommandHandler(() => OnClick("leaf"), true));
            }
        }

        private ICommand _tableCommand;
        public ICommand TableCommand
        {
            get
            {
                return _tableCommand ?? (_tableCommand = new CommandHandler(() => OnClick("table"), true));
            }
        }

        private ICommand _AddJoinCommand;
        public ICommand AddJoinCommand
        {
            get
            {
                return _AddJoinCommand ?? (_AddJoinCommand = new CommandHandler(() => OnAddJoin(), true));
            }
        }

        public void OnClick(string action)
        {
            if (action == "leaf")
            {
                OnShowLeaf();
            }
            else
            {
                OnShowTable();
            }
        }

        public event EventHandler showLeaf;
        public event EventHandler showTable;
        public event EventHandler AddJoin;

        public void OnShowLeaf()
        {
            if (showLeaf != null)
            {
                showLeaf(this, new EventArgs());
            }
        }

        public void OnAddJoin()
        {
            if (AddJoin != null)
            {
                AddJoin(this, new EventArgs());
            }
        }

        public void OnShowTable()
        {
            if (showTable != null)
            {
                showTable(this, new EventArgs());
            }
        }

        public string Id
        {
            get
            {
                return address;
            }
        }

        public Node Clone()
        {
            Node clone = new Node();

            clone.nodeId = this.nodeId;
            clone.address = this.address;
            clone.leaf = this.leaf;
            
            clone.MissingFromTable = this.MissingFromTable;
            clone.MissingFromLeaf = this.MissingFromLeaf;
            clone.missingNode = this.missingNode;
            clone.Color = this.Color;

            if (this.table != null)
                clone.table = this.table;

            return clone;
        }
    }
}
